package com.yb0os1.context;

import com.yb0os1.beans.BeanFactory;
import com.yb0os1.beans.SimpleBeanFactory;
import com.yb0os1.core.ClassPathXmlResource;
import com.yb0os1.core.XmlBeanDefinitionReader;
import com.yb0os1.exception.BeanException;

public class ClassPathXmlApplicationContext {
    private final BeanFactory beanFactory;
    //context负责整个容器的启动 读取外部配置 解析bean定义 创建beanFactory
    public ClassPathXmlApplicationContext(String fileName) {
        ClassPathXmlResource classPathXmlResource = new ClassPathXmlResource(fileName);
        beanFactory = new SimpleBeanFactory();
        XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
        xmlBeanDefinitionReader.loadBeanDefinitions(classPathXmlResource);
    }

    public Object getBean(String name) throws BeanException {
        return beanFactory.getBean(name);
    }
}
